package com.example.demo.Task.hbutetc;

import com.example.demo.utils.VerifyCodeUtil;
import lombok.Data;
import org.apache.http.Header;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import java.util.ArrayList;
import java.util.List;

/**
 * 登陆湖北工业大学工程技术学院
 * @author lizijian
 */
@Data
public class Login implements Runnable {

    private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36";
    private static final String LOGIN_URL = "http://202.114.190.75/j_spring_security_check";
    private String username;
    private String password;
    private volatile boolean finish;
    private CloseableHttpClient httpClient;
    public Login(String username, String password) {
        this.username = username;
        this.password = password;
    }

    @Override
    public void run() {
        try {
            //如果有线程完成任务，提前终止其他线程
            if (finish){
                return;
            }
            CookieStore cookieStore = new BasicCookieStore();
            CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
            String verifyCode = VerifyCodeUtil.getHbutEtcVerifyCode(httpClient);
            //如果有线程完成任务，提前终止其他线程
            if (finish){
                return;
            }
            System.out.println("start:");
            HttpPost httpPost = new HttpPost(LOGIN_URL);
            httpPost.addHeader("User-Agent", USER_AGENT);
            List<NameValuePair> pairList = new ArrayList<>(3);
            pairList.add(new BasicNameValuePair("j_username", username));
            pairList.add(new BasicNameValuePair("j_password", password));
            pairList.add(new BasicNameValuePair("j_captcha", verifyCode));
            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(pairList);
            httpPost.setEntity(urlEncodedFormEntity);
            CloseableHttpResponse response = httpClient.execute(httpPost);
            Header[] locations = response.getHeaders("Location");
            boolean loginSuccess = "Location: http://202.114.190.75/".equals(locations[0].toString());
            if (loginSuccess) {
                if (!finish){
                    synchronized (this){
                        if(!finish){
                            finish=true;
                            this.httpClient=httpClient;
                            this.notify();
                            System.out.println("success");
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
